You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Prevent no-op replacements from corrupting CRLF files - #1772
When every str_replace pair is skipped because the old and new strings are identical, currentContent still contains the original CRLF bytes. Restoring CRLF before checking for changes turns each \r\n into \r\r\n. The runtime then sends a patch and reports success despite making no requested edit.
Changes
Check for content changes with normalized line endings before restoring the file's line endings. A no-op returns the existing error result without creating a patch.
Change the existing identical-pair regression to use CRLF. It fails on unmodified main and passes with the fix; no additional test cases or files are added.
Verification
Bun 1.3.14: the regression file changes from 31 pass / 1 fail on main to 32 pass / 0 fail.
Related replacement, indentation, bounded-patch and file-processing tests: 40 pass / 0 fail across 4 files.
A temporary-file probe runs processStrReplace → postStreamProcessing → SDK changeFile: main writes once and produces CRCRLF; this patch performs zero writes and preserves the bytes. This is a file-processing probe, not an interactive CLI test.
Runtime and SDK typechecks pass with the installed native TypeScript 7.0.2 compiler. bun install --frozen-lockfile --no-env-file reports no changes. git diff --check passes.
ESLint: 0 errors, 4 existing TypeScript resolver warnings. Prettier's whole-file check fails on the checkout; comparison with main after normalizing checkout line endings shows no new formatting differences.
SDK and Freebuff builds are blocked by the existing dts-bundle-generator / TypeScript resolution error (ts.sys.getCurrentDirectory is undefined), also recorded on unmodified main. Binary version/smoke checks were not run because no binary was built.
Scope / Tradeoffs
Source-audit fix; no existing issue is claimed. Successful replacements retain the current line-ending policy, including normalization of mixed-ending files. No-op detection ignores CRLF/LF differences, consistent with the existing replacement matching and identical-pair checks. Matching heuristics and client-side patch application are unchanged. Related PR #1291 guards whitespace-only fuzzy matches; this fix addresses the later no-op/line-ending boundary.
Good catch. The bug is real: currentContent.replaceAll('\n', lineEnding) was applied before the no-op check, so a file with CRLF line endings that goes through processStrReplace with an identical old/new pair would still be converted and then falsely compared against the un-normalized initialContent, producing \r\r\n and a spurious successful patch. Moving the line-ending restoration to after the no-op check, and doing the no-op comparison via normalizeLineEndings on both sides, is the right fix and keeps the scope minimal (packages/agent-runtime/src/process-str-replace.ts).
The updated test in process-str-replace.test.ts (switching the identical-pair fixture from \n to \r\n) is a reasonable regression test and demonstrates the fix directly - good that you showed it fails on main and passes with the patch.
Two things I'd want double-checked before this lands upstream:
Confirm normalizeLineEndings is already imported/available in this file (the diff doesn't show an import change, so it must already exist - worth confirming in your PR description since it's load-bearing for compilation).
It might be worth an additional test case with a genuinely mixed-ending file that does contain a real replacement, to confirm the successful-replacement path still normalizes correctly post-fix, since the PR only adds coverage for the no-op case.
Overall this is a small, well-scoped, well-reasoned bug fix in an in-scope package. Worth a maintainer porting it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem / Why
When every
str_replacepair is skipped because the old and new strings are identical,currentContentstill contains the original CRLF bytes. Restoring CRLF before checking for changes turns each\r\ninto\r\r\n. The runtime then sends a patch and reports success despite making no requested edit.Changes
Verification
processStrReplace→postStreamProcessing→ SDKchangeFile: main writes once and produces CRCRLF; this patch performs zero writes and preserves the bytes. This is a file-processing probe, not an interactive CLI test.bun install --frozen-lockfile --no-env-filereports no changes.git diff --checkpasses.dts-bundle-generator/ TypeScript resolution error (ts.sys.getCurrentDirectoryis undefined), also recorded on unmodified main. Binary version/smoke checks were not run because no binary was built.Scope / Tradeoffs
Source-audit fix; no existing issue is claimed. Successful replacements retain the current line-ending policy, including normalization of mixed-ending files. No-op detection ignores CRLF/LF differences, consistent with the existing replacement matching and identical-pair checks. Matching heuristics and client-side patch application are unchanged. Related PR #1291 guards whitespace-only fuzzy matches; this fix addresses the later no-op/line-ending boundary.